/**
 * Created by zhao on 17-1-23.
 */
var selectModule=-1;
var onSelectModule;
var selectTitle="";

//-------------------功能方法------------------------

function getPageSize() {
    return $('#tb').datagrid('getPager').pagination('options').pageSize;
}

//高级查询
function advancedQuery() {
    selectModule = onSelectModule;
    selectTitle = $("#queryTitle").val();
    //重新加载数据

    $('#tb').datagrid('load', {
        startIndex: '0',
        pageSize: getPageSize(),
        //高级搜索条件
        selectModule:selectModule,
        selectTitle:selectTitle
    });


}


//点击编辑调用该方法
function editNews(id){
    createEditNewsPanelOnEast(false, id);
}

function createNews() {
    createEditNewsPanelOnEast(true, null);
}

//确认是否删除
function isRemoveNews(id) {
    $.messager.confirm('确认','您确认想要删除ID为 '+id+' 的文章吗？',function(r){
        if (r){
            removeNews(id);
        }
    });
}
//删除news
function removeNews(id) {
    $.ajax("/admin/news/removeNews.do?id="+id,{
        success:function (data) {
            $('#tb').datagrid('reload');
            $.messager.alert('警告','删除成功');
        },
        error:function () {
            $('#tb').datagrid('reload');
            $.messager.alert('警告','删除失败');
        }
    });
    //更新数据表
}
/**
 * 创建编辑和添加文章panel 在east
 * @param isCreateNews 是否是添加
 * @param id 如果为编辑，传入编辑news id
 */
function createEditNewsPanelOnEast(isCreateNews, id){
    var title;
    var iconCls;
    var url;
    if (isCreateNews){
        //新增news模式
        title = '发布新文章';
        url = '/admin/news/createNewsUi.do';
        iconCls = 'icon-add';
        createPanelOnEast(title, iconCls, url);
    }else{
        //编辑news模式
        title = "编辑文章 id#"+id;
        url = '/admin/news/editNewsUi.do?id='+id;
        iconCls = 'easy-edit';
        window.open(url, "_blank");
    }

}

//改变文章的置顶级别
function updateOrderCount(id,orderCount ){
    $.ajax("/admin/news/updateOrderCount.do",{
        type: "POST",
        data: "id="+id+"&orderCount="+orderCount,
        success: function(msg){
            $.messager.alert('NOTE',msg);
        },
        error:function () {
            $.messager.alert('NOTE',"更改置顶级别失败!");
        }
    })
}

//-------------------渲染流程------------------------
var count = 0;
//news的总数


$(
    function () {
    //create datagrid
    $('#tb').datagrid({
//                fit:true,
        url: '/admin/news/getNewsByPage.do',
        queryParams: {
            startIndex: '0',
            pageSize: '10',
            //高级搜索条件
            selectModule:selectModule,
            selectTitle:selectTitle
        },
        pagePosition: top,
        pagination: true,
        toolbar: '#toolbar',
        columns: [[{field: 'id', title: 'id'},
            {field: 'title', title: '标题'},
            {field: 'author', title: '作者'},
            {field: 'date', title: '发布日期'},
            {field: 'intrduce', title: '内容简介'},
            {
                field: 'module.name', title: '模块', formatter: function (value, row, index) {
                console.log(row);
                if (row.module==null){
                    return null;
                }
                return row.module.name;
            }
            },
            {field: 'order', title: '置顶级别',formatter:function (value, row, index) {
                var select = "<select name='orderCount' onchange='updateOrderCount("+row.id+",this.options[this.options.selectedIndex].value)'>";
                for (var i=0;i<=10;i++){
                    if (row.orderCount==i){
                        select+="<option selected='true' value='"+i+"'>"+i+"</option>";
                    }else{
                        select+="<option  value='"+i+"'>"+i+"</option>"
                    }
                }
                select+="</select>";
                return select;
            }},
            {
                field:'read' ,title:'查看',formatter:function (value, row, index) {
                return "<a href='/news/read.do?id="+row.id+"' target='blank' >查看</a>";
            }
            },

            {
                field: 'edit', title: '编辑', formatter: function (value, row, index) {
                //点击修改触发editNews
                return "<a href='#'  onclick='editNews(" + row.id + ")'>修改</a>";
            }
            },
            {field: 'remove', title: '删除',formatter:function (value, row, index) {
                return "<a href='#' class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-search'\" onclick='isRemoveNews(" + row.id + ")'>删除</a>";
            }}
        ]
        ]
    });

    $('#tb').datagrid('getPager').pagination({
        onSelectPage: function (pageNo, pageSize) {
            var start = (pageNo - 1) * pageSize;
            //异步获取
            $('#tb').datagrid('reload',{
                startIndex: start,
                pageSize: pageSize,
                selectModule:selectModule,
                selectTitle:selectTitle
            })
        }
    })
});